<?php

namespace Common\Controller;

/**
 * 类说明
 *
 * @author chengwei wang
 */
class SmsController {
    private $url = 'http://112.74.76.186:8030/service/httpService/httpInterface.do';  //请求地址
    private $username='JSM40947';  //用户名
    private $password='MT123qwe,./';     //密码
    private $tempid='JSM40947-0001';    //模板ID
    private $veryCode='l9057qjtnse0';    //通讯认证Key
    
    /*
     * mobile   目标手机号码
     * types    1：注册；2：找回密码；3：重置手机；  默认1
     * platfrom 1：手机；2：PC                      默认1
     */    
    public function send($mobile,$types=1,$yzcode=1) {

        if(empty($mobile)){
            return '你崽儿尽然不输入手机号码也想发短信？';
        }

        $rand=mt_rand(100000, 999999);
        
        $params = array(
            'username'=>$this->username,
            'password'=>$this->password,
            'veryCode'=>$this->veryCode,
            'method'=>'sendMsg',
            'mobile'=>$mobile,
            'content'=>"@1@=".$rand,
            'msgtype'=>'2',
            'tempid'=>$this->tempid,
            'code'=>'utf-8'            
        );
        
        //$verification = md5($yzcode);
        
        if(session('?verification')){
            request_post($this->url, $params);
            $this->savesms($mobile, $rand,1,$types,1);
            return true;
        }else{
            return false;
        }
    }
    
    private function GetIP(){
        if(!empty($_SERVER["HTTP_CLIENT_IP"])){
          $cip = $_SERVER["HTTP_CLIENT_IP"];
        }
        elseif(!empty($_SERVER["HTTP_X_FORWARDED_FOR"])){
          $cip = $_SERVER["HTTP_X_FORWARDED_FOR"];
        }
        elseif(!empty($_SERVER["REMOTE_ADDR"])){
          $cip = $_SERVER["REMOTE_ADDR"];
        }
        else{
          $cip = "无法获取！";
        }
        return $cip;
    }

    /*
     * mobile   目标手机号码
     * contents 发送内容
     * types    1：注册；2：找回密码；3：重置手机；  默认1
     * platfrom 1：手机；2：PC                      默认1
     * status   是否发送成功  1 成功； 0 失败
     * 
     */
    
    private function savesms($mobile,$contents,$status,$types=1,$platfrom=1){
            $sql="INSERT INTO mt_mobile_sms (mobile,times,types,platfrom,contents,status) VALUES ('".$mobile."','".date('Y-m-d H:i:s',  time())."',".$types.",".$platfrom.",'".$contents."',".$status.")";
            M('MobileSms')->query($sql);      
    }
    
    


}
